/*
    This file is part of ciberRatoToolsSrc.

    Copyright (C) 2001-2006 Universidade de Aveiro

    ciberRatoToolsSrc is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    ciberRatoToolsSrc is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include "cbparamdialog.h"

#include <stdio.h>

#include <qlineedit.h>
#include <qcheckbox.h>

#include "cbparameters.h"


/* 
 *  Constructs a cbParamDialog which is a child of 'parent', with the 
 *  name 'name' and widget flags set to 'f' 
 *
 *  The dialog will by default be modeless, unless you set 'modal' to
 *  TRUE to construct a modal dialog.
 */
cbParamDialog::cbParamDialog( cbParameters *par, QWidget* parent,  const char* name, bool modal, WFlags fl )
    : cbParamDialogBase( parent, name, modal, fl )
{
	char strNum[200];

	param = par;

	sprintf(strNum,"%d",param->simTime);
	lnEditSimTime->setText(strNum);
	sprintf(strNum,"%d",param->runningTimeout);
	lnEditRunningTimeout->setText(strNum);
	sprintf(strNum,"%d",param->cycleTime);
	lnEditSimStep->setText(strNum);

	//Noise
	sprintf(strNum,"%5.2f",param->obstacleNoise);
	lnEditObstacleNoise->setText(strNum);
	sprintf(strNum,"%5.2f",param->compassNoise);
	lnEditCompassNoise->setText(strNum);
	sprintf(strNum,"%5.2f",param->beaconNoise);
	lnEditBeaconNoise->setText(strNum);
	sprintf(strNum,"%5.2f",param->motorsNoise);
	lnEditMotorsNoise->setText(strNum);

	//Requests
	sprintf(strNum,"%3d",param->nReqPerCycle);
	lnEditReqPCycle->setText(strNum);
	cBoxReqIR->setChecked(param->obstacleRequestable);
	cBoxReqBeacon->setChecked(param->beaconRequestable);
	cBoxReqCompass->setChecked(param->compassRequestable);
	cBoxReqGround->setChecked(param->groundRequestable);
	cBoxReqCollision->setChecked(param->collisionRequestable);

	//Latencies
	sprintf(strNum,"%3d",param->obstacleLatency);
	lnEditLatIR->setText(strNum);
	sprintf(strNum,"%3d",param->compassLatency);
	lnEditLatCompass->setText(strNum);
	sprintf(strNum,"%3d",param->beaconLatency);
	lnEditLatBeacon->setText(strNum);
	sprintf(strNum,"%3d",param->groundLatency);
	lnEditLatGround->setText(strNum);
	sprintf(strNum,"%3d",param->collisionLatency);
	lnEditLatCollision->setText(strNum);

	connect((QObject *)buttonOk, SIGNAL(clicked()), SLOT(accept()));
}

void cbParamDialog::accept(void)
{
	int    vali;
	double valf; 
	char   ch;

	if(sscanf(lnEditSimTime->text(),"%d%c",&vali,&ch)==1)
	    param->simTime = vali;
	if(sscanf(lnEditRunningTimeout->text(),"%d%c",&vali,&ch)==1)
	    param->runningTimeout = vali;
	if(sscanf(lnEditSimStep->text(),"%d%c",&vali,&ch)==1)
	    param->cycleTime = vali;

	//Noise
	if(sscanf(lnEditObstacleNoise->text(),"%lf%c",&valf,&ch)==1)
	    param->obstacleNoise = valf;
	if(sscanf(lnEditCompassNoise->text(),"%lf%c",&valf,&ch)==1)
	    param->compassNoise = valf;
	if(sscanf(lnEditBeaconNoise->text(),"%lf%c",&valf,&ch)==1)
	    param->beaconNoise = valf;
	if(sscanf(lnEditMotorsNoise->text(),"%lf%c",&valf,&ch)==1)
	    param->motorsNoise = valf;

	//Requests
	if(sscanf(lnEditReqPCycle->text(),"%d%c",&vali,&ch)==1)
	    param->nReqPerCycle = vali;
        param->obstacleRequestable = cBoxReqIR->isChecked();
        param->compassRequestable = cBoxReqCompass->isChecked();
        param->beaconRequestable = cBoxReqBeacon->isChecked();
        param->groundRequestable = cBoxReqGround->isChecked();
        param->collisionRequestable = cBoxReqCollision->isChecked();

	//Latencies
	if(sscanf(lnEditLatIR->text(),"%d%c",&vali,&ch)==1)
	    param->obstacleLatency = vali;
	if(sscanf(lnEditLatCompass->text(),"%d%c",&vali,&ch)==1)
	    param->compassLatency = vali;
	if(sscanf(lnEditLatBeacon->text(),"%d%c",&vali,&ch)==1)
	    param->beaconLatency = vali;
	if(sscanf(lnEditLatGround->text(),"%d%c",&vali,&ch)==1)
	    param->groundLatency = vali;
	if(sscanf(lnEditLatCollision->text(),"%d%c",&vali,&ch)==1)
	    param->collisionLatency = vali;

	cbParamDialogBase::accept();
}

/*  
 *  Destroys the object and frees any allocated resources
 */
cbParamDialog::~cbParamDialog()
{
    // no need to delete child widgets, Qt does it all for us
}

